package com.vq.controller;

import com.vq.common.R;
import com.vq.entity.CommunityMember;
import com.vq.entity.User;
import com.vq.service.CommunityMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/CommunityMember")
public class CommunityMemberController {
    @Autowired
    HttpSession session;

    @Autowired
    CommunityMemberService communityMemberService;

    @PostMapping("/{communityId}")
    public R insert(@PathVariable("communityId") Long communityId) {
        User user = (User) session.getAttribute("myUser");
        boolean bo = communityMemberService.insert(user.getAccount(), communityId);
        return new R(bo?1:0);
    }

    /**
     * 退出社区
     * @return
     */
    @DeleteMapping("/{communityId}")
    public R quit(@PathVariable("communityId") Long communityId) {
        User user = (User) session.getAttribute("myUser");
        boolean bo = communityMemberService.deleteByCommunityId(user.getAccount(), communityId);
        return new R(bo?1:0);
    }

    @GetMapping("/{account}")
    public R selectByAccount(@PathVariable("account") String account) {
        List<CommunityMember> communityMemberList = communityMemberService.selectByAccount(account);
        return new R(1, communityMemberList);
    }
}
